﻿using System;
using System.Data;
using System.Configuration;
using System.Text;
using System.Security.Cryptography;


/// <summary>
/// MD5 的摘要说明
/// </summary>
public class MD5
{
    public MD5()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }

    #region 密码加密处理
    /// <summary>
    /// 字节转换方法，将字符串转换为bite型
    /// </summary>
    /// <param name="chars">用户输入的字符串</param>
    /// <returns></returns>
    public byte[] EncordingReverse(string chars)
    {
        SHA1Managed mydata = new SHA1Managed();
        byte[] bites = new byte[20];
        ASCIIEncoding pw = new ASCIIEncoding();
        bites = pw.GetBytes(chars);
        bites = mydata.ComputeHash(bites);
        return bites;
    }

    /// <summary>
    /// MD5加密，并转换为字符串
    /// </summary>
    /// <param name="str">待加密字符串</param>
    /// <returns>加密后的字符串</returns>
    public byte[] GetMD5(string str)
    {
        byte[] b = System.Text.Encoding.Default.GetBytes(str);
        //System.Text.ASCIIEncoding asc = new ASCIIEncoding();
        //int count = asc.GetBytes(str, 0, str.Length, b, 0);
        //b = new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(b, 0, count);
        //return b;
        StringBuilder sb = new StringBuilder(64);
        StringBuilder ss = new StringBuilder(64);
        foreach (byte bs in System.Text.Encoding.Default.GetBytes(str))
        {
            sb.Append(bs);
        }

        return EncordingReverse(sb.ToString());
    }
    #endregion
}
